﻿namespace CodeGenerator.CSharp
{
    /// <summary>
    /// Defines a code unit for a procedure.
    /// </summary>
    public class Procedure : CodeUnit
    {
        public Procedure(string name, ModifierCollection modifiers) : base(name, modifiers) { }
        public Procedure(string name, ModifierCollection modifiers, string dataType) : base(name, modifiers) 
        { this.DataType = dataType; }

        public override string ToString()
        {
            CodeBuilder Code = new CodeBuilder();

            this.AppendDocumentation(Code);
            this.AppendAttributes(Code);

            string CloseIfAbstractOrPartial = (this.Modifiers.IsAbstract || this.Modifiers.IsPartial) ? ";" : string.Empty;
            
            string LocalDataType = this.HasDataType() ? this.DataType : "void";
            Code.Add("{0}{1} {2} {3}({4}){5}", this.Padding, this.Modifiers, LocalDataType, this.Name, this.Parameters, CloseIfAbstractOrPartial);
            if (CloseIfAbstractOrPartial != ";")
            {
                Code.Add(this.Padding + "{");
                Code.Add(this.Body);
                Code.Add(this.Padding + "}");
            }

            return Code.ToString();
        }

    }
}
